import os
from CalTorque import CalTorque
import math
import datetime
import numpy as NP
import matplotlib
matplotlib.use('agg')
from matplotlib import pyplot as PLT
from matplotlib import dates

def PlotCalTorque(position, torque, time, tube, run, direction, filedirection, source, station, outdir):

   fig = PLT.figure(figsize=(15,8),dpi=150)
   box = [0.14, 0.14, 0.76, 0.76]
   ax1 = fig.add_axes(box)
   ax1.set_ylabel('Position (cm)',color='b')
   ax2 = ax1.twinx()
   ax2.set_ylabel('Torque (in.oz)', color='g')
   ax1.set_xlabel('Time (s)')
   maxx = max(time)
   minx = max(time)*0.07
   ax1.hlines(338.0,minx,maxx,color='0.82',linewidth=4)
   ax1.annotate('S2',xy=(minx*.7,338.0),color='0.75')
   if max(position)> 350:
      ax1.hlines(388.0,minx,maxx,color='0.82',linewidth=4)
      ax1.annotate('S5',xy=(minx*.7,388.0),color='0.75')
   if max(position)> 400:
      ax1.hlines(442.0,minx,maxx,color='0.82',linewidth=4)
      ax1.annotate('S8',xy=(minx*.7,442.0),color='0.75')
   if max(position)> 450:
      ax1.hlines(497.0,minx,maxx,color='0.82',linewidth=4)
      ax1.annotate('S11',xy=(minx*.7,497.0),color='0.75')
   if max(position)> 530:
      ax1.hlines(548.0,minx,maxx,color='0.82',linewidth=4)
      ax1.annotate('S14',xy=(minx*.7,548.0),color='0.75')
   PLT.title(run + ' Source ' + source + ' ' + direction + ' Torque vs Time')
   ax1.grid()
   ax1.plot(time, position, '-', color='blue',linewidth=2)
   ax2.plot(time, torque, '-', color='green',linewidth=2)
   PLT.savefig(outdir + run + '_' + filedirection + '_time.png')
   print "  Saving",run, direction 

   # -- SECOND PLOT --
   fig2 = PLT.figure(figsize=(15,8),dpi=150)
   ax21 = fig2.add_axes(box)
   ax21.set_ylabel('Bend Angle',color='b')
   ax22 = ax21.twinx()
   ax22.set_ylabel('Torque (in.oz)', color='g')
   ax21.set_xlabel('Distance (cm)')
   miny = math.floor(min(torque) / 5) * 5
   ymax = max(torque)*0.5
   maxx = max(position)
   
   ## Add station locations
   if maxx >= 328.9:
        ax22.axvline(338.9, color = 'r', linewidth = 1, linestyle = '-')
        ax22.annotate('S2', xy = (340.9, miny * 0.8), color = 'r')
   if maxx >= 382.6:
        ax22.axvline(392.6, color = 'r', linewidth = 1, linestyle = '-')
        ax22.annotate('S5', xy = (394.6, miny * 0.8), color = 'r')
   if maxx >= 435.7:
        ax22.axvline(445.7, color = 'r', linewidth = 1, linestyle = '-')
        ax22.annotate('S8', xy = (447.7, miny * 0.8), color = 'r')
   if maxx >= 491.7:
        ax22.axvline(501.7, color = 'r', linewidth = 1, linestyle = '-')
        ax22.annotate('S11', xy = (503.7, miny * 0.8), color = 'r')
   if maxx >= 595.8:
        ax22.axvline(605.8, color = 'r', linewidth = 1, linestyle = '-')
        ax22.annotate('S17', xy = (607.8, miny * 0.8), color = 'r')
 
   ## Add bend locations
   if maxx >= 47.8:
        ax22.axvspan(47.8, 93.5, color = 'g', alpha = 0.2)
        ax22.annotate('Bellows', xy = (48, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 136.4:
        ax22.axvspan(136.4, 144.6, color = 'g', alpha = 0.2)
        ax22.annotate('First outer bend', xy = (136, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 196.5:
        ax22.axvspan(196.5, 218.0, color = 'g', alpha = 0.2)
        ax22.annotate('The "S"', xy = (222, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 228.3:
        ax22.axvspan(228.3, 241.5, color = 'g', alpha = 0.2)
   if maxx >= 274.6:
        ax22.axvspan(274.6, 284.6, color = 'g', alpha = 0.2)
        ax22.annotate('Bend A5', xy = (274, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 309.0:
        ax22.axvspan(309.0, 318.9, color = 'g', alpha = 0.2)
        ax22.annotate('Bend A6', xy = (309, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 358.8:
        ax22.axvspan(358.8, 370.8, color = 'g', alpha = 0.2)
        ax22.annotate('Bend A7', xy = (359, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 414.5:
        ax22.axvspan(414.5, 426.5, color = 'g', alpha = 0.2)
        ax22.annotate('Bend A8', xy = (414, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 445.3:
        ax22.axvspan(445.3, 455.3, color = 'g', alpha = 0.2)
        ax22.annotate('Bend A9', xy = (445, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 467.5:
        ax22.axvspan(467.5, 479.5, color = 'g', alpha = 0.2)
        ax22.annotate('Bend A10', xy = (467, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 523.8:
       ax22.axvspan(523.8, 535.8, color = 'g', alpha = 0.2)
       ax22.annotate('Bend A11', xy = (523, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 571.6:
       ax22.axvspan(571.6, 583.6, color = 'g', alpha = 0.2)
       ax22.annotate('Bend A12', xy = (571, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 627.9:
       ax22.axvspan(627.9, 639.9, color = 'g', alpha = 0.2)
       ax22.annotate('Bend A13', xy = (627, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 652.7:
       ax22.axvspan(652.7, 660.7, color = 'g', alpha = 0.2)
       ax22.annotate('Bend A14', xy = (652, miny), color = 'g', rotation = 'vertical', va = 'bottom')
   if maxx >= 678.5:
       ax22.axvspan(678.5, 681.1, color = 'g', alpha = 0.2)
       ax22.annotate('Bend A15', xy = (678, miny), color = 'g', rotation = 'vertical', va = 'bottom')
        
   PLT.title(run + ' Source ' + source + ' ' + direction + station + ' Torque vs Position')
   ax21.grid()
   ax22.plot(position, torque, '-', color='g',linewidth=2)
   ax21.plot(tube['x'],tube['y'], '-', color='b',linewidth=2)
   fig2.savefig(outdir + run + '_' + filedirection + '_bend_angle' + '.png')

